package com.zq.recruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zq.common.exception.ResultException;
import com.zq.recruit.dao.entity.Company;
import com.zq.recruit.dao.entity.DeliveryRecord;
import com.zq.recruit.dao.entity.Interview;
import com.zq.recruit.dao.entity.JobPosition;
import com.zq.recruit.mapper.InterviewMapper;
import com.zq.recruit.service.CompanyService;
import com.zq.recruit.service.DeliveryRecordService;
import com.zq.recruit.service.InterviewService;
import com.zq.recruit.service.JobPositionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 张乔
* @description 针对表【interview(面试安排表)】的数据库操作Service实现
* @createDate 2025-04-05 19:14:49
*/
@Service
public class InterviewServiceImpl extends ServiceImpl<InterviewMapper, Interview>
    implements InterviewService{


    @Resource
    private InterviewMapper interviewMapper;


    @Override
    public void getExistId(Interview interview) {
    Integer count=   interviewMapper.getExistId(interview);
    if(count>0){
        throw new ResultException(501,"该记录已存在，请勿重复添加");
    }
    }


    @Resource
    private DeliveryRecordService deliveryRecordService;

    @Resource
    private JobPositionService jobPositionService;

    @Resource
    private CompanyService companyService;



    @Override
    public List<Interview> getInfoByUserId(Integer userId) {
        LambdaQueryWrapper<DeliveryRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId!=null,DeliveryRecord::getUserId, userId);
        List<DeliveryRecord> list = deliveryRecordService.list(queryWrapper);
        if(list==null||list.isEmpty()){
            return List.of();
        }
List<Integer> ids = list.stream().map(DeliveryRecord::getDeliveryId).toList();
   List<Interview> interviewList=  interviewMapper.getInfoByIds(ids);
   for (Interview interview : interviewList){
       DeliveryRecord byId = deliveryRecordService.getById(interview.getDeliveryId());
       JobPosition byId1 = jobPositionService.getById(byId.getJobId());
if(byId1!=null){
    Company byId2 = companyService.getById(byId1.getCompanyId());
interview.setJobName(byId1.getPositionName());
interview.setCompanyName(byId2.getCompanyName());
interview.setCompanyPath(byId2.getImageUrl());
interview.setSalaryRange(byId1.getSalaryRange());
}
   }
        return interviewList;
    }

    @Override
    public List<Interview> getAllInfo() {
        List<Interview> interviewList = this.list();
        for (Interview interview : interviewList){
            DeliveryRecord byId = deliveryRecordService.getById(interview.getDeliveryId());
            JobPosition byId1 = jobPositionService.getById(byId.getJobId());
            if(byId1!=null){
                Company byId2 = companyService.getById(byId1.getCompanyId());
                interview.setJobName(byId1.getPositionName());
                interview.setCompanyName(byId2.getCompanyName());
                interview.setCompanyPath(byId2.getImageUrl());
                interview.setSalaryRange(byId1.getSalaryRange());
                interview.setFilePath(byId.getResumeUrl());
            }
        }
        return interviewList;
    }


}




